package com.example.asus_pc.myshop.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.example.asus_pc.myshop.R;
import com.example.asus_pc.myshop.bean.VegaBean;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class EditVegaNumAdatpter extends RecyclerView.Adapter<EditVegaNumAdatpter.ItemViewHolder> {

    private LayoutInflater mInflater;
    Context context;

    List<VegaBean> datas;

    public EditVegaNumAdatpter(Context context) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        datas = new ArrayList<>();
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.item_edit_vega_count, null);

        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ItemViewHolder holder, final int i) {
        holder.tvNO.setVisibility(View.VISIBLE);
        holder.etTotalNum.setVisibility(View.VISIBLE);
        holder.etSplitNum.setVisibility(View.VISIBLE);

        holder.tvNO.setText(i + 1 + "");
        holder.tvVeganame.setText(datas.get(i).getVegaName());
        holder.etTotalNum.setHint(datas.get(i).getTotalPrice()+"元/斤");
        holder.etTotalNum.setText(datas.get(i).getTotalNum() == 0 ? "" : datas.get(i).getTotalNum() + "");
        holder.etSplitNum.setHint(datas.get(i).getSplitPrice()+"元/斤");
        holder.etSplitNum.setText(datas.get(i).getSplitNum() == 0 ? "" : datas.get(i).getSplitNum() + "");

        final TextWatcher totalWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s.toString())
                        || s.toString().equals("-")
                        || s.toString().equals("-0")
                        || s.toString().equals("-0.")
                        || s.toString().equals("0")
                        || s.toString().equals("0.")
                        || s.toString().equals(".")
                        ) {
                    datas.get(i).setTotalNum(0);
                } else {
                    datas.get(i).setTotalNum(new BigDecimal(s.toString()).doubleValue());
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        };
        final TextWatcher splitWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence editable, int start, int before, int count) {
                if (TextUtils.isEmpty(editable.toString())
                        || editable.toString().equals("-")
                        || editable.toString().equals("-0")
                        || editable.toString().equals("-0.")
                        || editable.toString().equals("0")
                        || editable.toString().equals("0.")
                        || editable.toString().equals(".")
                        ) {
                    datas.get(i).setSplitNum(0);
                } else {
                    datas.get(i).setSplitNum(new BigDecimal(editable.toString()).doubleValue());
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        };
        View.OnFocusChangeListener totalListener = new View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    holder.etTotalNum.addTextChangedListener(totalWatcher);

                }else{
                    holder.etTotalNum.removeTextChangedListener(totalWatcher);
                }
            }
        };
        View.OnFocusChangeListener spliteListener = new View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    holder.etSplitNum.addTextChangedListener(splitWatcher);
                }else{
                    holder.etSplitNum.removeTextChangedListener(splitWatcher);
                }
            }
        };
        holder.etTotalNum.setOnFocusChangeListener(totalListener);
        holder.etSplitNum.setOnFocusChangeListener(spliteListener);

    }

    @Override
    public int getItemCount() {
        return datas.size();
    }


    public List<VegaBean> getDatas() {
        return datas;
    }

    public void setDatas(List<VegaBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }


    static class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView tvNO;
        TextView tvVeganame;
        EditText etTotalNum;
        EditText etSplitNum;


        public ItemViewHolder(View itemView) {
            super(itemView);
            tvNO = (TextView) itemView.findViewById(R.id.tv_order_number);
            tvVeganame = (TextView) itemView.findViewById(R.id.tv_vega_name);
            etTotalNum = (EditText) itemView.findViewById(R.id.et_total_price);
            etSplitNum = (EditText) itemView.findViewById(R.id.et_splite_price);

        }

    }


}
